Rustでは、 列挙型 (enums)は単なるリスト以上のものであり、可能性の建築設計図です。ゆるくグループ化された整数とは異なり、列挙型は 和型を意味します。つまり、変数は複数の異なる バリエーションのいずれか一つを正確に表現できます。
1. 名前空間とスコープ
バリエーションは、二重コロン(::)演算子を使って列挙型の識別子の下に整理されています。この 名前空間 は、異なる モジュール、 クレート、または パッケージ間で衝突を防ぎます。これにより、複数の文脈で V4 というバリエーションを明確な曖昧さなく定義できます。
2. 型安全
関数の引数として列挙型を使用することで、 標準ライブラリ パターンにより、有効な状態のみがロジックに入ることを保証します。これにより、潜在的なランタイムエラーをコンパイル時へ移動させ、 route 関数が存在しない「v5」アドレスを処理する必要がないことを保証します。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which operator is used to access a variant within an enum's namespace?
The dot operator (
.)The double colon (
::)The arrow operator (
->)The ampersand (
&)✅ Correct!
Correct! The double colon links the enum type to its specific variant scope.❌ Incorrect
Rust uses the double colon (::) for namespacing variants under their enum identifier.QUESTION 2
Why are enums considered 'Type Safe' in function arguments?
They allow any integer to be passed as long as it is positive.
They force the compiler to check that only predefined variants are used.
They automatically encrypt the data.
They convert strings to integers automatically.
✅ Correct!
Exactly. The compiler guarantees that only legitimate variants can be passed, eliminating invalid input cases.❌ Incorrect
Enums restrict values to a predefined set, catching errors during compilation rather than at runtime.QUESTION 3
In the IP address example, what is the primary benefit of using an enum over a String?
Strings use less memory.
Strings are easier to type.
Enums eliminate the need for 'invalid version' error handling in the function body.
Enums allow for infinite variants.
✅ Correct!
Because the type is constrained to V4 or V6, the function logic is simplified.❌ Incorrect
Using Strings requires manual validation; Enums provide validation through the type system itself.QUESTION 4
What happens if you try to use a variant name that hasn't been defined in the enum?
The program crashes at runtime.
The compiler throws a 'variant not found' error.
Rust creates the variant automatically.
It defaults to the first variant.
✅ Correct!
Correct. Rust is strict about its defined types within a scope.❌ Incorrect
Rust's compiler ensures that every identifier used matches a known definition in the current scope.QUESTION 5
Can two different enums in the same module have variants with the same name?
No, it causes a naming conflict.
Yes, because they are namespaced under their respective enum identifiers.
Only if they are inside a struct.
Only if one is private.
✅ Correct!
Yes! Color::Red and Stoplight::Red are distinct because of namespacing.❌ Incorrect
Namespacing allows the same variant name to exist across different types without conflict.Module Architecture Case Study
Applying Enums to API Design
You are designing a cross-platform file system library. You need to represent the status of a file operation: Success, Failure (with an error code), or Pending. You are deciding between using a set of constants or a single Enum.
Q
How does using an Enum improve the maintainability of your API across different Crates?
Solution:
Using an Enum ensures that any external user of the Crate is forced to handle the specific variants you've defined. It provides a single source of truth for possible states, and namespacing prevents these states from conflicting with the user's local variables.
Using an Enum ensures that any external user of the Crate is forced to handle the specific variants you've defined. It provides a single source of truth for possible states, and namespacing prevents these states from conflicting with the user's local variables.
Q
What is the architectural advantage of Namespacing in this scenario?
Solution:
Namespacing allows you to use clear, concise names like
Namespacing allows you to use clear, concise names like
Status::Pending without worrying that 'Pending' is already used as a variable name in the main program. It encapsulates the logic within the type's own scope.